rm(list = setdiff(ls(), lsf.str()))

load("Study 2/data_trump.RData")
###SSI Sample-----------------
#Trump
Trump<-lm(zero1(Trumpfav)~zero1(Agre)+zero1(authoritarianism)+sex+age+as.factor(race)+as.factor(education)+zero1(income01) + partyidentity,data=data_trump)
#Ryan
Ryan<-lm(zero1(Ryanfav)~zero1(Agre)+zero1(authoritarianism)+sex+age+as.factor(race)+as.factor(education)+zero1(income01) + partyidentity,data=data_trump)
#Clinton
Clinton<-lm(zero1(Clintonfav)~zero1(Agre)+zero1(authoritarianism)+sex+age+as.factor(race)+as.factor(education)+zero1(income01) + partyidentity,data=data_trump)
#Obama
Obama<-lm(zero1(Obamafav)~zero1(Agre)+zero1(authoritarianism)+sex+age+as.factor(race)+as.factor(education)+zero1(income01) + partyidentity,data=data_trump)

#make table with results ---------
favorability.tables <- stargazer(Trump, Ryan,Clinton, Obama,
                                 title="Candidate favorability", align=TRUE, 
                                 omit.stat=c("LL","ser","f", "adj.rsq"), 
                                 star.cutoffs=c(0.05), 
                                 covariate.labels = c("Agreeableness", "Authoritarianism", "Female", "Age", "Black", "American Indian", "Asian","Native Hawain", "Other",  "High school graduate", "Some college", "2 years degree", "4 years degree", "Professional degree", "Doctorate", "Income", "Partisanship"),
                                 column.labels = c("Trump", "Ryan", "Clinton","Obama"), dep.var.labels.include = FALSE,
                                 notes = "Unstandardized OLS regression coefficients; *p<0.05", label = "tab:SSI_results", 
                                 notes.append = FALSE, no.space=TRUE, out="Tables/SSI_favorability.tex")

#Make a plot for SSI sample --------------------
favorability <- data.frame(rbind(summary(Trump)$coefficients[c(2:2),1:2], summary(Ryan)$coefficients[c(2:2),1:2], summary(Clinton)$coefficients[c(2:2),1:2], summary(Obama)$coefficients[c(2:2),1:2]))
favorability$candidate <- factor(c("Trump","Ryan","Clinton","Obama"), levels=c("Trump","Ryan","Clinton","Obama"))
favorability$trait <- c(rep("Agreeableness",4))
colnames(favorability)[2]="se"
favorability$study<-"SSI sample"

favorability_auth <- data.frame(rbind(summary(Trump)$coefficients[c(3:3),1:2], summary(Ryan)$coefficients[c(3:3),1:2], summary(Clinton)$coefficients[c(3:3),1:2], summary(Obama)$coefficients[c(3:3),1:2]))
favorability_auth$candidate <- factor(c("Trump","Ryan","Clinton","Obama"), levels=c("Trump","Ryan","Clinton","Obama"))
favorability_auth$trait <- c(rep("Authoritarianism",4))
colnames(favorability_auth)[2]="se"
favorability_auth$study<-"SSI sample"


###ANES pre-election----------------------------
load("Study 1/Altered Data/Study 1_US_ANES2016.Rdata")

#Preelection: Feeling towards Trump
Trump <- lm(zero1(pre_feel_trump) ~ zero1(agreeableness) + zero1(authoritarianism) + zero1(openness) + zero1(conscientiousness) +  zero1(extraversion) +  zero1(neuroticism) +  female + age +  black + Indian + Asian + Hawaiian + Other + as.factor(education) + income + income_missing + partyidentity, data=data)
#Preelection: Feeling towards Republican party
Rep <- lm(zero1(pre_feel_rep_party) ~ zero1(agreeableness) + zero1(authoritarianism) + zero1(openness) + zero1(conscientiousness) +  zero1(extraversion) +  zero1(neuroticism)  +  female + age +  black + Indian + Asian + Hawaiian + Other + as.factor(education) + income + income_missing + partyidentity, data=data)
#Preelection: Feeling towards Democratic party
Dem<- lm(zero1(pre_feel_dem_party) ~ zero1(agreeableness) + zero1(authoritarianism) + zero1(openness) + zero1(conscientiousness) +  zero1(extraversion) +  zero1(neuroticism) +  female + age +  black + Indian + Asian + Hawaiian + Other + as.factor(education) + income + income_missing + partyidentity, data=data)
#Preelection: Feeling towards Hillary
Clinton <- lm(zero1(pre_feel_hillary) ~ zero1(agreeableness) + zero1(authoritarianism) + zero1(openness) + zero1(conscientiousness) +  zero1(extraversion) +  zero1(neuroticism) +  female + age +  black + Indian + Asian + Hawaiian + Other + as.factor(education) + income + income_missing + partyidentity, data=data)
#Preelection: Feeling towards Obama
Obama <- lm(zero1(pre_feel_obama) ~ zero1(agreeableness) + zero1(authoritarianism) + zero1(openness) + zero1(conscientiousness) +  zero1(extraversion) +  zero1(neuroticism) +  female + age +  black + Indian + Asian + Hawaiian + Other + as.factor(education) + income + income_missing + partyidentity, data=data)

#Make a plot for Agreeableness and Candidate support ---------------
favANES <- data.frame(rbind(summary(Trump)$coefficients[c(2:2),1:2], summary(Rep)$coefficients[c(2:2),1:2], summary(Clinton)$coefficients[c(2:2),1:2], summary(Obama)$coefficients[c(2:2),1:2], summary(Dem)$coefficients[c(2:2),1:2]))
favANES$candidate <- factor(c("Trump","Republican Party","Clinton","Obama", "Democratic Party"), levels=c("Trump","Republican Party","Clinton","Obama", "Democratic Party"))
favANES$trait <- c(rep("Agreeableness",5))
colnames(favANES)[2]="se"
favANES$study<-"ANES 2016\n pre-election"

#Make a plot for Authoritarianism and Candidate support ---------------
favANES_auth <- data.frame(rbind(summary(Trump)$coefficients[c(3:3),1:2], summary(Rep)$coefficients[c(3:3),1:2], summary(Clinton)$coefficients[c(3:3),1:2], summary(Obama)$coefficients[c(3:3),1:2], summary(Dem)$coefficients[c(3:3),1:2]))
favANES_auth$candidate <- factor(c("Trump","Republican Party","Clinton","Obama", "Democratic Party"), levels=c("Trump","Republican Party","Clinton","Obama", "Democratic Party"))
favANES_auth$trait <- c(rep("Authoritarianism",5))
colnames(favANES_auth)[2]="se"
favANES_auth$study<-"ANES 2016\n pre-election"


###ANES post-election----------------------------
Trump_post <- lm(zero1(post_feel_trump) ~ zero1(agreeableness) + zero1(authoritarianism) + zero1(openness) + zero1(conscientiousness) +  zero1(extraversion) +  zero1(neuroticism) +  female + age +  black + Indian + Asian + Hawaiian + Other + as.factor(education) + income + income_missing + partyidentity, data=data)

#post election: feeling towards Hillary
Hillary_post <- lm(zero1(post_feel_hillary) ~  zero1(agreeableness) + zero1(authoritarianism) + zero1(openness) + zero1(conscientiousness) +  zero1(extraversion) +  zero1(neuroticism)+  female + age +  black + Indian + Asian + Hawaiian + Other + as.factor(education) + income + income_missing + partyidentity, data=data)


#Make a plot for Agreeableness and Candidate support ---------------
favANES_post <- data.frame(rbind(summary(Trump_post)$coefficients[c(2:2),1:2], summary(Hillary_post)$coefficients[c(2:2),1:2]))
favANES_post$candidate <- factor(c("Trump","Clinton"), levels=c("Trump","Clinton"))
favANES_post$trait <- c(rep("Agreeableness",2))
colnames(favANES_post)[2]="se"
favANES_post$study<-"ANES 2016\n post-election"

#Make a plot for Authoritarianism and Candidate support ---------------
favANES_post_auth <- data.frame(rbind(summary(Trump_post)$coefficients[c(3:3),1:2], summary(Hillary_post)$coefficients[c(3:3),1:2]))
favANES_post_auth$candidate <- factor(c("Trump","Clinton"), levels=c("Trump","Clinton"))
favANES_post_auth$trait <- c(rep("Authoritarianism",2))
colnames(favANES_post_auth)[2]="se"
favANES_post_auth$study<-"ANES 2016\n post-election"

#Make plot
comb_fav<-rbind(favorability, favANES, favANES_post)
comb_fav$study <-factor(comb_fav$study, levels=c('ANES 2016\n pre-election', 'ANES 2016\n post-election', 'SSI sample'))
comb_fav$candidate <-factor(comb_fav$candidate, levels=c('Trump', 'Ryan', 'Republican Party', 'Clinton', 'Obama', 'Democratic Party'))
comb_fav_auth<-rbind(favorability_auth, favANES_auth, favANES_post_auth)
comb_fav_auth$study <-factor(comb_fav_auth$study, levels=c('ANES 2016\n pre-election', 'ANES 2016\n post-election', 'SSI sample'))
comb_fav_auth$candidate <-factor(comb_fav_auth$candidate, levels=c('Trump', 'Ryan', 'Republican Party', 'Clinton', 'Obama', 'Democratic Party'))

comb_all<-rbind(comb_fav, comb_fav_auth)
comb_all$trait <-factor(comb_all$trait , levels=c('Agreeableness', 'Authoritarianism'))



fig1 <- ggplot(comb_all ,aes(x=candidate, y=Estimate, colour=candidate))+geom_pointrange(aes(ymin=Estimate-1.96*se,ymax=Estimate+1.96*se), size=1) +  ylim(-.3,.3) + theme_bw()+theme(legend.position="off")+ylab("Politician favorability")+xlab("")+geom_hline(yintercept=0)+ theme(axis.text.x=element_text(size=14, angle=45, hjust = 1), axis.text.y=element_text(size=14), strip.text.x=element_text(size=14), strip.text.y=element_text(size=14), axis.title=element_text(size=14)) + scale_colour_manual(values=c("red", "blue", "blue", "blue", "blue", "blue")) +  facet_grid(trait~study, scales = "free_x") 

ggsave(fig1, file="Figures/fig_appendixA16.pdf", dpi=900, width = 8, height =6)
